package demo2;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:接口使用案例之Comparator接口(比较器)
 * User: LianBao
 * Date: 2024-03-09
 * Time: 16:51
 */
class Student implements Comparable<Student> {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }


    //Comparator接口中有一个compare方法

}


public class Test2 {

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("ZhangSan", 10);
        students[1] = new Student("lisi", 5);
        students[2] = new Student("WangWu", 15);
        NameParator nameParator = new NameParator();
        Arrays.sort(students, nameParator);
        System.out.println(Arrays.toString(students));
    }

    public static void main1(String[] args) {
        Student student1 = new Student("ZhangSan", 10);
        Student student2 = new Student("LiSi", 15);

        NameParator nameParator = new NameParator();
        //调用NameParator中的compare方法
        nameParator.compare(student1, student2);


    }

}
